<template>
  <section class="padding EndWaybillList">
    <el-card class="margin-top">
      <div class=" padding-bottom-sm flex align-center justify-between">
        <el-pagination
          :current-page="currentPage"
          :page-sizes="[20, 40, 60, 80, 100]"
          :page-size="pageSize"
          layout="prev, pager, next, jumper, sizes,total"
          :total="pageTotal"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />

        <el-button-group>
          <el-button
            type="success"
            size="mini"
            icon="el-icon-plus"
            @click="dialogFormVisible = true"
          >
            客户文件邮件通知
          </el-button>
        </el-button-group>
      </div>
      <el-table
        class="table-box"
        :data="tableData"
        style="width: 100%"
        size="mini"
      >
        <ex-table-column
          v-for="column in tableColumns"
          :key="column.prop"
          :auto-fit="true"
          :fit-header="true"
          :prop="column.prop"
          :label="column.label"
        />
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-tooltip
              class="item margin-left-sm"
              effect="dark"
              content="编辑"
              placement="top"
            >
              <i
                class="el-icon-edit-outline"
                style="font-size: large;"
                @click="colEdit(scope.row)"
              />
            </el-tooltip>
            <el-tooltip
              class="item margin-left-sm"
              effect="dark"
              content="删除"
              placement="top"
            >
              <i
                class="el-icon-close"
                style="font-size: large;"
                @click="colDel(scope.row.id)"
              />
            </el-tooltip>
          </template>
        </el-table-column>
      </el-table>
    </el-card>

    <el-dialog
      title="客户文件邮件通知"
      width="30%"
      :visible.sync="dialogFormVisible"
    >
      <el-form
        ref="ruleForm"
        :rules="rules"
        label-position="left"
        label-width="76px"
        size="mini"
        :model="form"
        class="padding-lr-40"
      >
        <el-form-item prop="email">
          <div slot="label">邮箱:</div>
          <el-input v-model="form.email" placeholder="请填写邮箱" clearable />
        </el-form-item>
        <el-form-item prop="fileType">
          <div slot="label">文件类型:</div>
          <el-select v-model="form.fileType" placeholder="请选择文件类型">
            <el-option
              v-for="(label, value) in publicData.fileTypeMap"
              :key="value"
              :label="label"
              :value="value"
            />
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="resetForm('ruleForm')">
          取消
        </el-button>
        <el-button type="primary" @click="submitForm('ruleForm')">
          确定
        </el-button>
      </div>
    </el-dialog>
  </section>
</template>

<script>
import { mapGetters } from 'vuex'
import {
  customerFileNoticeEmailSave,
  customerFileNoticeEmailList,
  customerFileNoticeEmailDelete
} from '@/api/myProfile'

export default {
  name: 'CustomerProfileEmailNotification',

  data() {
    return {
      form: {
        email: '',
        fileType: ''
      },
      dialogFormVisible: false,

      currentPage: 1,
      pageSize: 20,
      pageTotal: 0,
      tableColumns: [
        { prop: 'email', label: '邮箱' },
        { prop: 'typeName', label: '通知文件类型' },
        { prop: 'cdate', label: '创建时间' }
      ],
      tableData: [],

      rules: {
        fileType: [
          { required: true, message: '请选择文件类型', trigger: 'change' }
        ],
        email: [
          { required: true, message: '请填写邮箱', trigger: 'blur' },
          { type: 'email', message: '请填写正确的邮箱地址', trigger: ['blur'] }
        ]
      }
    }
  },
  computed: {
    ...mapGetters(['publicData'])
  },
  mounted() {
    this.queryList()
  },
  methods: {
    async queryList() {
      this.$store.dispatch('app/toggleDialog', true)
      const dataResult = await customerFileNoticeEmailList({
        pageSize: this.pageSize,
        currentPage: this.currentPage
      })

      this.tableData = dataResult.datas
      this.tableData.forEach(data => {
        data.typeName = this.publicData.fileTypeMap[data.fileType]
      })
      this.pageTotal = dataResult.count
      this.$store.dispatch('app/toggleDialog', false)
    },
    handleSizeChange(val) {
      this.pageSize = val
      this.queryList()
    },
    handleCurrentChange(val) {
      this.currentPage = val
      this.queryList()
    },

    submitForm(formName) {
      this.$refs[formName].validate(valid => {
        if (valid) {
          this.saveForm(formName)
        } else {
          return false
        }
      })
    },
    async saveForm(formName) {
      this.$store.dispatch('app/toggleDialog', true)
      const data = await customerFileNoticeEmailSave(this.form)
      if (data.code === 200) {
        this.$message.success('保存成功')
        this.queryList()
        this.resetForm(formName)
      } else {
        this.$message.error(this.$t('customer.' + data.message))
      }
      this.$store.dispatch('app/toggleDialog', false)
    },
    resetForm(formName) {
      this.form = {
        email: '',
        fileType: ''
      }
      this.dialogFormVisible = false
    },
    colEdit(row) {
      this.form = row
      this.dialogFormVisible = true
    },
    colDel(id) {
      this.$confirm('确定要删除吗？')
        .then(_ => {
          this.delteRow(id)
        })
        .catch(_ => {})
    },
    async delteRow(id) {
      this.$store.dispatch('app/toggleDialog', true)
      const data = await customerFileNoticeEmailDelete(id)
      if (data.code === 200) {
        this.$message.success('删除成功')
        this.queryList()
        this.resetForm('ruleForm')
      } else {
        this.$message.error(data.message)
      }
      this.$store.dispatch('app/toggleDialog', false)
    }
  }
}
</script>

<style scoped>
.EndWaybillList >>> .el-textarea__inner {
  min-height: 60px !important;
}
</style>

<style lang="scss" scoped></style>
